Ext.define("Triends.Post",{
	extend: "Ext.data.Model",
	config: {
		idProperty: 'Number', //pullrefresh에서 중복된 값이 바인딩 되지 않도록 하기 위함!
		fields: ["Number", "ID", "WRITER", "TITLE", "CONTENT", "CONTINENT", "NATION", "DATE", "TIME", "HHMM"]
	}
});

var postStore = Ext.create("Ext.data.Store",{
	model: "Triends.Post",
	id: 'poststore',
	pageSize: 10,
	clearOnPageLoad: false,
	proxy: {
		type: "jsonp",
		url: 'http://itscreater.cafe24.com/Triends/Postlist.jsp',
		callbackkey: 'callback',
		pageParam: 'pages',
		limitParam: 'rpp',
		reader: {
			type: "json",
			rootProperty: "results",
			totalProperty: "total"
		}	
	},
	sorters: [
		{
			property: 'DATE',
			direction: 'DESC'
		},
		{
			property: 'TIME',
			direction: 'DESC'
		}
	]
});


Ext.define('Triends.view.PostNormalList', {
	requires: ['Triends.view.SearchOverlay', 'Ext.plugin.ListPaging', 'Ext.plugin.PullRefresh'],
	extend: 'Ext.dataview.List',
	xtype: 'postnormallist',
	id: 'postnormallist',
	config: {
		plugins: [
			{type: 'listpaging', autoPaging:true, clearOnPageLoad: false, loadMoreText: 'Next..'},
			{type: 'pullrefresh' /*refreshFn: function(plugin){postStore.load()}*/}
		],
		fullscreen: true,
		store: postStore,
		limit: 10,
		hidden: true,
		pressedCls: "x-item-selected",
		selectedCls: "",
		pressedDelay: 50,
		itemTpl: "<b>{TITLE}</b><br><font size='2'>{WRITER}&nbsp;&nbsp;|&nbsp;&nbsp;{DATE}&nbsp;&nbsp;&nbsp;{HHMM}</font>",
		listeners: 
			{
				itemtap: function(list, index, target, model, e, eOpts) {
	        	 //logout버튼으로 login후에 사용가능
	        	try {
	        		
	        		var userid = Ext.getStore('UserStore').getAt(0).data.ID;
	        		var postnum = model.getData().Number;
	        		
		        	if (model.data.ID == userid) {
		        		//Postinfo 수정, 삭제가능
		        		var postdelbtn = Ext.getCmp('postnormaldeletebtn');
		        		var postmdfbtn = Ext.getCmp('postnormalmodifybtn');
		        		postdelbtn.setHidden(false);
		        		postmdfbtn.setHidden(false);
		        		
		        	} else {
		        		//Postinfo 수정, 삭제불가능
		        		var postdelbtn = Ext.getCmp('postnormaldeletebtn');
		        		var postmdfbtn = Ext.getCmp('postnormalmodifybtn');
		        		postdelbtn.setHidden(true);
		        		postmdfbtn.setHidden(true);
		        	}
		        	var postnormallist = Ext.getCmp("postnormallist");
		        	var postinfopanel1 = Ext.getCmp('postinfopanel1');
		        	var postinfopanel2 = Ext.getCmp('postinfopanel2');
		        	var postcontentpanel = Ext.getCmp('postcontentpanel');
		        	postinfopanel1.setData(model.data);
		        	postinfopanel2.setData(model.data);
		        	postcontentpanel.setData(model.data);
		        					
		        	var PostNormalCommentStore = Ext.getStore('PostNormalCommentStore');
	 					PostNormalCommentStore.getProxy().setExtraParams({num: postnum});
	 					PostNormalCommentStore.load({
	 						callback: function() {
								Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
            						url: 'mainpage/post/postinfo'
	        					}));					        	
	 						}
						}); 
	        	} catch(e){
	        		Ext.Msg.alert("","로그인이 필요합니다");
	        	}	        	
	        },
	        
	        initialize: function(){
				var postStore = Ext.getStore('poststore');
				postStore.load();
			}
	    },
		items: [
			{
			xtype: 'toolbar',
			docked: 'top',
			items: [
			{
				xtype: 'spacer'
			},
			{
				xtype: 'button',
				id: 'searchbutton',
				text: '검색',
				height: '20px',
				iconCls: 'search',
				iconMask: true,
				handler: function(btn){
					var searchoverlay = Ext.getCmp("searchoverlay");
					searchoverlay.showBy(btn);
				}	
			},
			{
				xtype: 'button',
				id: 'writebutton',
				text: '글쓰기',
				height: '20px',
				iconCls: 'compose',
				iconMask: true,
				handler: function() {
					try{
						var userid = Ext.getStore("UserStore").getAt(0).data.ID;
						Triends.app.getHistory().add(Ext.create('Ext.app.Action', {
            						url: 'mainpage/post/postingnormal'
	        					}));	
					} catch(e){
						Ext.Msg.alert("","로그인이 필요합니다");
					}
				}
			}]
		}]
		
	}
});


    				
    				
    			